package demo1;
import javax.swing.*;
import java.awt.*;
/**
 * 测试网格布局
 * 注意网格布局是以行数优先
 * @author 15716
 *
 */
public class TestGridLayout {
	
	public static void main(String[] args) {
		JFrame jf = new JFrame();
		jf.setSize(300,400);
		jf.setTitle("网格布局");
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setResizable(false);
		jf.setLocationRelativeTo(null);
		
		//FlowLayout layout1 = new FlowLayout();
		//方法一
		GridLayout layout = new GridLayout(3,3,10,10);
		//方法二
		//layout.setHgap(10);//设置水平间隙
		//layout.setVgap(10);//设置竖直间隙;
		
		//创建中间容器
		JPanel panel = new JPanel(layout);
		
		//创建按钮
//		JButton button1 = new JButton("这是按钮1");
//		JButton button2= new JButton("这是按钮2");
//		JButton button3 = new JButton("这是按钮3");
//		JButton button4 = new JButton("这是按钮4");
//		JButton button5 = new JButton("这是按钮5");
//		JButton button6 = new JButton("这是按钮6");
//		JButton button7 = new JButton("这是按钮7");
//		JButton button8 = new JButton("这是按钮8");
//		JButton button9 = new JButton("这是按钮9");
		
		//将按钮添加到中间容器中
		for(int i = 1; i <= 9; ++i) {
			JButton button = new JButton("按钮" + i);
			panel.add(button);
		}
		//将中间容器添加到窗口中
		jf.setContentPane(panel);
		jf.setVisible(true);
	}

}
